//
//  ViewController.swift
//  EXSwift
//
//  Created by Jiafan on 12/16/15.
//  Copyright © 2015 Jiafan. All rights reserved.
//

import UIKit
import CoreLocation

class ViewController: UIViewController,CLLocationManagerDelegate {
    
    let locationManager:CLLocationManager = CLLocationManager()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        NSUTF8StringEncoding
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    
    func compare(s1:String,s2:String) -> Bool{
        return s1 < s2;
    }
    
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
        print(locations)
    }
    
    func locationManager(manager: CLLocationManager, didFailWithError error: NSError){
        print(error)
    }
    
    @IBAction func getLocation(sender: UIButton) {
        if  CLLocationManager.locationServicesEnabled(){
            print("normal")
        }else{
            print("not allowed")
        }
        
        
        switch CLLocationManager.authorizationStatus(){
        case .NotDetermined :
        if self.locationManager.respondsToSelector("requestWhenInUseAuthorization"){
            self.locationManager.requestWhenInUseAuthorization()
        }
            
        if self.locationManager.respondsToSelector("requestAlwaysAuthorization") {
            self.locationManager.requestAlwaysAuthorization()
        }
            
        case .Denied :
            displayAlertWithTitle("Denied", message: "Location services are not allowed for this app")
            
        case .Restricted :
            displayAlertWithTitle("Restricted", message: "Location services are not allowed for this app")
        case .AuthorizedAlways:
            displayAlertWithTitle("AuthorizedAlways", message: "Location services are not allowed for this app")
        case .AuthorizedWhenInUse:
            displayAlertWithTitle("AuthorizedWhenInUse", message: "Location services are not allowed for this app")
//        default:
//            displayAlertWithTitle("default", message: "Location services are not allowed for this app")
        }
        
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = 50
        locationManager.startUpdatingLocation()
    }
    
    func displayAlertWithTitle(title: String, message: String)
    {
        let controller = UIAlertController(title: title, message: message, preferredStyle: .Alert)
        controller.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        presentViewController(controller, animated: true, completion: nil)
    }
}

